import 'package:flutter/cupertino.dart';
import 'package:razer_one/app/main/model/status_model.dart';
import 'package:razer_one/app/main/view/main_status_view.dart';

class MainStatusContainer extends StatefulWidget {
  final StatusModel statusModel; //状态模型
  final Function? netErrorHandle; //网络错误
  final Function? downRefreshHandle; //下拉刷新
  final Function? upRefreshHandle; //上拉刷新
  final Function? emptyBtnHandle; //空数据按钮回调

  final String emptyText; //空数据显示的文字
  final String emptyBtnTitle; //空数据按钮显示文字
  // final bool isShowEmpty;
  final Widget child;
  const MainStatusContainer({
    Key? key,
    required this.statusModel,
    this.netErrorHandle,
    this.downRefreshHandle,
    this.upRefreshHandle,
    this.emptyBtnHandle,
    this.emptyText = "暂时没有更多数据",
    this.emptyBtnTitle = "前往添加",
    // this.isShowEmpty = false,
    required this.child,
  }) : super(key: key);

  @override
  State<MainStatusContainer> createState() => _MainStatusContainerState();
}

class _MainStatusContainerState extends State<MainStatusContainer> {
  double _topMargin = 0;
  String _refreshStr = "下拉可以刷新";

  @override
  Widget build(BuildContext context) {
    return NotificationListener(
      onNotification: (ScrollNotification notification) {
        var maxScrollExtent = notification.metrics.maxScrollExtent;
        var pixels = notification.metrics.pixels;
        var dis = maxScrollExtent - pixels;

        //下拉刷新
        if (widget.downRefreshHandle != null &&
            pixels <= 300 &&
            !widget.statusModel.isDownRefreshing) {
          setState(() {
            _topMargin = -pixels;
            _refreshStr = -pixels > 70 ? "松开立即刷新" : "下拉可以刷新";
          });
        }
        //上拉刷新 如果数据没有在加载中,并且有更多数据 才
        if (dis <= 100 &&
            !widget.statusModel.isLoading &&
            widget.statusModel.isHaveMore) {
          widget.statusModel.isLoading = true;
          if (widget.upRefreshHandle != null) widget.upRefreshHandle!();
        }
        return true;
      },
      child: Stack(
        children: [
          Padding(
            padding: EdgeInsets.only(
                top: widget.statusModel.isDownRefreshing ? 50 : 0),
            child: Listener(
              onPointerUp: onPointerUp,
              child: MainStatusView(
                statusModel: widget.statusModel,
                emptyText: widget.emptyText,
                emptyBtnHandle: widget.emptyBtnHandle,
                emptyBtnTitle: widget.emptyBtnTitle,
                netErrorHandle: () {
                  if (widget.netErrorHandle != null) widget.netErrorHandle!();
                },
                child: widget.child,
              ),
            ),
          ),
          //是否有下拉刷新
          Positioned(
              top: widget.statusModel.isDownRefreshing ? 20 : _topMargin - 30,
              left: 0,
              right: 0,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Visibility(
                      visible: widget.statusModel.isDownRefreshing,
                      child: const CupertinoActivityIndicator(
                        radius: 10,
                      )),
                  const SizedBox(
                    width: 10,
                  ),
                  Text(
                    _refreshStr,
                  )
                ],
              ))
        ],
      ),
    );
  }

  void onPointerUp(PointerUpEvent event) {
    if (widget.statusModel.isDownRefreshing) return;
    if (_topMargin > 70) {
      setState(() {
        widget.statusModel.isDownRefreshing = true;
        _topMargin = 0;
        _refreshStr = "正在刷新中";
        Future.delayed(const Duration(milliseconds: 200), () {
          widget.downRefreshHandle!();
        });
      });
    }
  }
}
